﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class PopForm
    {
        private Thread _thdPopForm = null;
        private TooltipForm _tooltipForm = new TooltipForm();
        private Form _parentForm = null;
        private Cursor _parentFormOriCursor = null;

        public void Show(Form parentForm, string message)
        {

            Close();

            if (parentForm != null)
            {
                _parentForm = parentForm;
                _parentFormOriCursor = _parentForm.Cursor;
                _parentForm.Cursor = Cursors.WaitCursor;
            }

            try
            {
                _thdPopForm = new Thread(PopTooltipForm);
                _thdPopForm.Start(message);
            }
            catch
            { }
        }

        public void Close()
        {
            try
            {
                if (_parentForm != null && _parentFormOriCursor != null)
                {
                    _parentForm.Cursor = _parentFormOriCursor;
                }

                if (_thdPopForm != null)
                {
                    _thdPopForm.Abort();
                    _thdPopForm = null;
                }

                if (_tooltipForm != null)
                    _tooltipForm.Close();
            }
            catch
            { }
        }

        private void PopTooltipForm(object obj)
        {
            if (_tooltipForm != null)
                _tooltipForm.Close();

            _tooltipForm = new TooltipForm(obj.ToString());
            _tooltipForm.ShowDialog();
        }

    }
}
